package com.example.pwj.ffstudy.ui.zhihu.presenter;

import com.example.pwj.ffstudy.base.BaseFragment;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyExtra;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyStoryDetail;
import com.example.pwj.ffstudy.ui.zhihu.contract.ZNewsDetailContract;
import com.example.pwj.ffstudy.ui.zhihu.model.ZNewsDetailModel;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pwj on 2017/8/25.
 */

public class ZNewsDetailPresenter implements ZNewsDetailContract.Presenter {
    private ZNewsDetailModel model;
    private ZNewsDetailContract.View view;
    @Inject
    public ZNewsDetailPresenter(ZNewsDetailModel model) {
        this.model = model;
    }

    @Override
    public void takeView(ZNewsDetailContract.View view) {
        this.view = view;
    }

    @Override
    public void dropView() {
        if (view != null) {
            view = null;
        }
    }

    @Override
    public void loadZDailyStoryDetail(int id) {
        model.getZDailyStoryDetail(id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(((BaseFragment)view).<ZDailyStoryDetail>bindToLifecycle())
                .subscribe(new Observer<ZDailyStoryDetail>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ZDailyStoryDetail zDailyStoryDetail) {
                        view.onLoadZDailyDetail(zDailyStoryDetail);
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError();
                    }

                    @Override
                    public void onComplete() {
                        view.onComplete();
                    }
                });
    }

    @Override
    public void loadZDailyExtra(int id) {
        model.getZDailyExtra(id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(((BaseFragment)view).<ZDailyExtra>bindToLifecycle())
                .subscribe(new Observer<ZDailyExtra>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(ZDailyExtra zDailyExtra) {
                        view.onLoadZDailyExtra(zDailyExtra);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}
